import {Matrix} from "./Matrix";
import {Fence} from "./fence";

/**
 * 栅栏分组
 * @author gcq
 * @date 2021-9-6
 */
class FenceGroup {
    spu
    skuList = []
    fences = []

    constructor(spu) {
        this.spu = spu
        this.skuList = spu.sku_list
    }

    getSku(skuCode) {
        const fullSkuCode = this.spu.id + '$' + skuCode
        const sku = this.spu.sku_list.find(s => s.code === fullSkuCode)
        return sku ? sku : null
    }

    /**
     * 获取默认sku
     * @returns {*}
     */
    getDefaultSku() {
        const defaultSkuId = this.spu.default_sku_id
        if (!defaultSkuId) {
            return
        }
        return this.skuList.find(i => i.id === defaultSkuId)
    }

    /**
     * 设置默认cell的状态
     * @param cellId
     * @param status
     */
    setCellStatusById(cellId, status) {
        this.eachCell((cell) => {
            if (cell.id == cellId) {
                cell.status = status
            }
        })
    }

    /**
     * 设置 cell 状态
     * @param x
     * @param y
     * @param status
     */
    setCellStatusByXY(x, y, status) {
        this.fences[x].cells[y].status = status
    }

    /**
     * 阅读性不是很好 过了一段时间后 理解起来有难度
     */
    initFence1() {
        const martix = this.createMatrix(this.skuList)
        const fences = []
        // 用于区分是否是一个新的列
        let currentJ = -1
        martix.each((element, i, j) => {
            if (currentJ !== j) {
                // 开启一个新列 需要创建一个新的Fence
                currentJ = j
                fences[currentJ] = this._createFence(element)
            }
            fences[currentJ].pushValueTitles(element.value)
        })
        console.log(fences)
    }

    _createFence(element) {
        const fence = new Fence()
        return fence
    }

    /**
     * 代码阅读性强 利用面向对象思维 封装性更好
     */
    initFence() {
        // 创建矩阵
        const matrix = this.createMatrix(this.skuList)
        const fences = []

        const AT = matrix.transpace();
        AT.forEach(r => {
            const fence = new Fence(r)
            fence.init()
            if (this._hasSketchFence() && this._isSketchFence(fence.id)) {
                fence.setFenceSketch(this.skuList)
            }
            fences.push(fence)
        })
        this.fences = fences
        console.log(this.fences)
    }

    /**
     * 判断是否有 sketch_spec_id
     * @returns {boolean}
     * @privat
     */
    _hasSketchFence() {
        return this.spu.sketch_spec_id ? true : false
    }

    /**
     *
     * @param fenceId
     * @returns {boolean}
     * @private
     */
    _isSketchFence(fenceId) {
        return this.spu.sketch_spec_id === fenceId ? true : false
    }

    /**
     * 循环遍历
     * @param cb 回调函数 返回当前 cell 行 i 列 j
     */
    eachCell(cb) {
        for (let i = 0; i < this.fences.length; i++) {
            for (let j = 0; j < this.fences[i].cells.length; j++) {
                const cell = this.fences[i].cells[j]
                cb(cell, i, j)
            }
        }
    }

    /**
     * 创建矩阵
     *  1、数学函数库
     *  2、不用 借助矩阵思维
     * @param skuList
     * @returns {Matrix}
     */
    createMatrix(skuList) {
        const m = []
        skuList.forEach(sku => {
            m.push(sku.specs)
        })
        return new Matrix(m)
    }
}

export {
    FenceGroup
}